import { createRouter, createWebHistory,createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import LocalCache from "@/libs/utils/cache";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/main",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/view/login/index.vue"),
  },
  {
    path: "/main",
    name: "main",
    component: () => import("@/view/main/index.vue")
  },
  {
    path: "/user",
    name: "user",
    component: () => import("@/view/user/index.vue")
  },
  {
    path: "/role",
    name: "role",
    component: () => import("@/view/role/index.vue")
  },
  {
    path: "/houseaddress",
    name: "houseaddress",
    component: () => import("@/view/address/index.vue")
  },
  {
    path: "/housecategory",
    name: "housecategory",
    component: () => import("@/view/category/index.vue")
  },
  {
    path: "/houselist",
    name: "houselist",
    component: () => import("@/view/house/index.vue")
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    const token = LocalCache.getCache("token");
    if (!token) {
      next("/login")
    } else {
      next();
    }
  }else {
    next()
  }
});

export default router;
